<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .big {
            width: 300px;
            height: 600px;
            display: flex;
            flex-direction: column;
            margin: 0 auto;
            margin-top: 50px;
            
        }

        input {
            margin-bottom: 30px;
            outline: none;
        }
    </style>
</head>

<body>
    <script src="lib/axios.js"></script>
    <div class="big">
        <input type="text" placeholder="请输入账号">
        <input type="text" placeholder="请输入密码">
        <input type="button" value="一键登录">
    </div>
    <script>
        class login {
            constructor() {
                this.username = document.querySelector("input:nth-of-type(1)")
                this.password = document.querySelector("input:nth-of-type(2)")
                this.geton = document.querySelector("input:nth-of-type(3)")
                // this.rules()
                this.bindevent()
            }
            bindevent() {
                this.geton.addEventListener("click", this.logingo.bind(this))
            }
            logingo() {
                let api = "http://localhost:8888"
                axios({
                    url: api + "/users/login",
                    method: "post",
                    headers: {
                        'Content-Type': "application/x-www-form-urlencoded"
                    },
                    data: {
                        username: this.username.value,
                        password: this.password.value
                    }
                }).then(res => {
                    // console.log(res.data);
                    if (res.data.code === 1) {
                        console.log(res.data.user);
                        sessionStorage.setItem("userdata", JSON.stringify(res.data.user))
                        sessionStorage.setItem("token", res.data.token)
                        console.log(res.data);
                        location.href = "10_购物车练习.html"
                    }
                    else {
                        alert(res.data.message)
                    }
                })
            }
        }
        new login()
    </script>
</body>

</html>